// pages/schedule/schedule.js
import * as dateUtil from '../../utils/date'
import { getInArray, indexOfArray } from '../../utils/util'
import service from '../../utils/service'

let app = getApp()
let today = new Date()   // 当前日期
let curFirstWeekDate // 当前周的第一天
let weekInfoList // 周信息列表
let curWeekIndex // 当前周索引
let weekCourseList // 周课程列表

//  格式化日期
let formatWeekDate = function (date) {
  return date.getDate() + '日'
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    todayIndex: -1, // 当天索引0-6，-1表示不在当前周
    curMonth: '', // 当前月份
    curWeek: '', // 当前周数
    curTitle: '', // 当前周标题
    courseColors: ['#ffca7f', '#91d7fd', '#96a4f9'], // 0学校课程 1其他课程 2智康课程
    colorArrays: [ "#85B8CF", "#90C652", "#D8AA5A", "#FC9F9D", "#0A9A84", "#61BC69", "#12AEF3", "#E29AAD","#e54d42", "#fbbd08","#39b54a" ],
    weekLabels: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    weekDates: [], // 周日期列表
    tasklist: [], // 课程列表
    grades: '',
    pregif: true
  },

  /**
   * 课程详情
   */
  showCardView: function (event) {
    let ele = event.currentTarget
    let type = ele.dataset.type
    let index = ele.dataset.index
    // index 表示 在 周课程列表数组中的 下标
    console.log(index)
    // app.globalData.currentCourse = weekCourseList[index]

    // if (type == 2) {
    //   wx.switchTab({
    //     url: '../queryCourse/queryCourse',
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: '../editCourse/editCourse',
    //   })
    // }
  },

  /**
   * 新建课程
   */
  // addCourseHandler: function (event) {
  //   wx.navigateTo({
  //     url: '../addCourse/addCourse',
  //   })
  // },

  /**
   * 上一周
   */
  prevWeekHandler: function (event) {
    // if (weekInfoList && curWeekIndex - 1 >= 0) {
    //   curWeekIndex--
    //   this.updateWeeks(dateUtil.getDiffDate(curFirstWeekDate, -7))
    // }

    // FIXME for demo
    this.updateWeeks(dateUtil.getDiffDate(curFirstWeekDate, -7))
  },

  /**
   * 下一周
   */
  nextWeekHandler: function (event) {
    // if (weekInfoList && curWeekIndex + 1 < weekInfoList.length) {
    //   curWeekIndex++
    //   this.updateWeeks(dateUtil.getDiffDate(curFirstWeekDate, 7))
    // }

    // FIXME for demo
    this.updateWeeks(dateUtil.getDiffDate(curFirstWeekDate, 7))
  },

  /**
   * 反馈
   */
  // feedbackHandler: function (event) {
  //   wx.navigateTo({
  //     url: '../feedback/feedback',
  //   })
  // },


  // 获取周课程
  updateWeeks: function (date) {

    console.log(date)
    wx.showLoading({
      title: '加载中',
    })

    // 判断是否当前天
    let todayIndex = -1
    if (date.getFullYear() == today.getFullYear() && date.getMonth() == today.getMonth() && dateUtil.getMonthWeekth(date) == dateUtil.getMonthWeekth(today)) {
      todayIndex = dateUtil.formatWeekOrder(today.getDay())
    }

    // 获取当前周日期
    curFirstWeekDate = dateUtil.getWeekFirstDate(date)
    let curMonth = curFirstWeekDate.getMonth() + 1
    let weekDates = []
    let tmpFirstDate = new Date(curFirstWeekDate)
    for (var i = 0; i < 7; i++) {
      if (i == 0) {
        weekDates.push(formatWeekDate(curFirstWeekDate))
      } else {
        weekDates.push(formatWeekDate(dateUtil.getDiffDate(tmpFirstDate, 1)))
      }
    }

    
    wx.cloud.callFunction(
      {
        // 需调用的云函数名
        name: 'getMyTimeTable',
        // 传给云函数的参数
        data: {
          week: dateUtil.getMonthWeekth(date).toString(),
          grades: this.data.grades,
        }
      }
    ).then(
      res =>{
        console.log('======',res)
        this.setData({
          tasklist: res.result.data
        }
        )
      }
    )
    

    // 获取当前周参数信息
    // let curWeekInfo = weekInfoList[curWeekIndex]
    // let curTitle = curWeekInfo.quterName
    // let curWeek = curWeekInfo.weekNumName
    // let curFirstWeekDateString = curWeekInfo.curDate

    // FIXME for demo
    let curWeek = '第' + dateUtil.getMonthWeekth(date) + '周'
    let curTitle = '20-21学年春季'

    // FIXME for demo
    // let tasklist = weekCourseList = [
    //   { grades: '软件工程1704',week: '1',day: 0, start: 1, sections: 2, course: "移动技术", teacher: "万立中", place: "图302" },
    //   { grades: '软件工程1704',week: '1',day: 0, start: 3, sections: 2, course: "移动技术", teacher: "万立中", place: "图302" },
    //   { grades: '软件工程1704',week: '1',day: 0, start: 7, sections: 2, course: "软件过程", teacher: "石军", place: "软303" },
    //   { grades: '软件工程1704',week: '1',day: 3, start: 1, sections: 2, course: "高等数学", teacher: "高文明", place: "软202" },
    //   { grades: '软件工程1704',week: '1',day: 1, start: 1, sections: 2, course: "高等数学", teacher: "高文明", place: "软202" },
    //   { grades: '软件工程1704',week: '1',day: 5, start: 3, sections: 2, course: "体育", teacher: "朱少华", place: "体育场" },
    //   { grades: '软件工程1704',week: '1',day: 4, start: 5, sections: 2, course: "大学英语", teacher: "高文明", place: "软202" },
    //   { grades: '软件工程1704',week: '1',day: 2, start: 3, sections: 2, course: "数据结构", teacher: "胡军", place: "软-420" },
    //   { grades: '软件工程1704',week: '1',day: 3, start: 3, sections: 2, course: "算法基础", teacher: "刘小东", place: "软-303" },
    //   { grades: '软件工程1704',week: '1',day: 5, start: 5, sections: 2, course: "大学物理", teacher: "辛勇", place: "软-115" },
    //   { grades: '软件工程1704',week: '1',day: 2, start: 7, sections: 2, course: "高等数学", teacher: "高文明", place: "软-202" },
    //   { grades: '软件工程1704',week: '1',day: 0, start: 5, sections: 2, course: "高等数学", teacher: "高文明", place: "软-202" },
    //   { grades: '软件工程1704',week: '1',day: 5, start: 5, sections: 2, course: "数字电路", teacher: "郑朝丹", place: "软-402" },
    //   { grades: '软件工程1704',week: '1',day: 4, start: 7, sections: 2, course: "数字电路", teacher: "郑朝丹", place: "软-402" },
    //   { grades: '软件工程1704',week: '1',day: 1, start: 9, sections: 2, course: "操作系统", teacher: "陈悦", place: "软-402" },
    //   { grades: '软件工程1704',week: '1',day: 3, start: 9, sections: 2, course: "C#开发", teacher: "企业", place: "软-402" },
    // ]
    
    this.setData({
      todayIndex,
      curMonth,
      curWeek,
      weekDates,
      curTitle
    })

    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('schedule page onLoad')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log('schedule page onReady')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log('schedule page onShow')

    

    // service.getWeekInfoList(data => {
    //   weekInfoList = data
    //   curWeekIndex = indexOfArray('selected', true, weekInfoList)
    //   this.updateWeeks(today)
    // })
    
    let userInfo  = wx.getStorageSync('userInfo')

    // console.log(userInfo)

    if(userInfo==''){
      this.setData({
        tasklist:[]
      })
      // console.log(this.data.tasklist)
      wx.hideLoading()
      
      
      this.setData({
        grades: ''
      }) 

      this.updateWeeks(today)
      wx.showToast({
        title: '未登录',
        icon: 'error',
        duration: 2000
      })

    }else{

      wx.showLoading({
        title: '加载中'
      })
      let myclass = userInfo.class
      this.setData({
        grades: myclass
      }) 

      this.updateWeeks(today)

      this.setData({
        pregif: false
      })
      wx.hideLoading({
        success: (res) => {},
      })
    }

    // console.log(this.data.grades)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // console.log('schedule page onPullDownRefresh')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // console.log('schedule page onShareAppMessage')
  }
})